package cn.zch.soft.util.task.gitee.repo.info;
import cn.zch.soft.model.gitee.api.get.v5.user.repos.Repos;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.util.HashMap;
import java.util.Map;
/**
 * @author zchcpywin10
 */
@Slf4j
public class TaskGetV5UserRepos {
  private static final String GITEE_REP_URL_BASE_ZCH = "https://gitee.com/api/v5/repos/zghnzch/";
  public static void main(String[] args) {
    getGiteeRepInfo();
  }
  public static void getGiteeRepInfo() {
    try {
      // 获取用户的仓库信息
      StringBuilder urlBase = new StringBuilder("https://gitee.com/api/v5/user/repos");
      String urlFull;
      StringBuilder requestParam = new StringBuilder();
      Map<String, String> requestParamMap = new HashMap<>();
      // access_token 9c062e7f7df104af536df7e796584b78 用户授权码 query string
      requestParamMap.put("access_token", "9c062e7f7df104af536df7e796584b78");
      // visibility	 公开(public)、私有(private)或者所有(all)，默认: 所有(all) query string
      requestParamMap.put("visibility", "all");
      // affiliation	 owner
      // owner(授权用户拥有的仓库)、collaborator(授权用户为仓库成员)、
      // organization_member(授权用户为仓库所在组织并有访问仓库权限)、enterprise_member(授权用户所在企业并有访问仓库权限)
      // 、admin(所有有权限的，包括所管理的组织中所有仓库、所管理的企业的所有仓库)。
      // 可以用逗号分隔符组合。如: owner, organization_member 或 owner, collaborator, organization_member query string
      requestParamMap.put("affiliation", "owner");
      // type 筛选用户仓库: 其创建(owner)、个人(personal)、其为成员(member)、公开(public)、私有(private)，
      // 不能与 visibility 或 affiliation 参数一并使用，否则会报 422 错误 query string
      requestParamMap.put("type", "");
      // sort 排序方式: 创建时间(created)，更新时间(updated)，最后推送时间(pushed)，仓库所属与名称(full_name)。默认: full_name query string
      requestParamMap.put("sort", "pushed");
      // direction 如果sort参数为full_name，用升序(asc)。否则降序(desc) query string
      requestParamMap.put("direction", "desc");
      // q 搜索关键字 query string
      requestParamMap.put("q", "");
      // page 1 当前的页码 query integer
      requestParamMap.put("page", "");
      // per_page 1000 每页的数量，最大为 100 query integer
      requestParamMap.put("per_page", "100");
      // "entrySet()" should be iterated when both the key and value are needed
      for (Map.Entry<String, String> entry : requestParamMap.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        // String value = requestParamMap.get(key)
        if (!StringUtils.isEmpty(value)) {
          requestParam.append(key).append("=").append(value).append("&");
        }
      }
      // log.info("requestParam = " + requestParam)
      urlBase.append("?").append(requestParam);
      // log.info("urlBase = " + urlBase)
      // i < Integer.MAX_VALUE 真实环境要用最大值 全部遍历
      for (int i = 1; i < Integer.MAX_VALUE; i++) {
        // log.info("第i = " + i + " 页")
        urlFull = urlBase + ("page=" + i);
        // log.info("urlFull = " + urlFull)
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        Request request = new Request.Builder().url(urlFull).method("GET", null).build();
        Response response = client.newCall(request).execute();
        // log.info("response = " + response)
        ResponseBody body = response.body();
        // log.info("body = " + body)
        String bodyString;
        if (body == null) {
          log.warn("列出授权用户的所有仓库 请求 返回结果 ResponseBody 为 null");
          break;
        }
        bodyString = body.string();
        // log.info("bodyString = " + bodyString)
        if (StringUtils.isEmpty(bodyString)) {
          log.warn("列出授权用户的所有仓库 请求 返回结果 body.string() 为空");
          break;
        }
        JSONArray jsonArray = JSON.parseArray(bodyString);
        if (jsonArray.isEmpty()) {
          log.warn("列出授权用户的所有仓库 请求 JSONArray 解析 返回结果 为空");
          break;
        }
        analysisGiteeRepInfo(jsonArray);
      }
    }
    catch (Exception e) {
      log.error(e.getLocalizedMessage());
    }
  }
  public static void analysisGiteeRepInfo(JSONArray jsonArray) {
    int size = jsonArray.size();
    // log.info("json array size=" + size)
    for (int j = 0; j < size; j++) {
      JSONObject jsonObject = jsonArray.getJSONObject(j);
      // log.info("jsonObject=" + jsonObject)
      if (jsonObject != null) {
        Repos repos2 = JSON.parseObject(JSON.toJSONString(jsonObject), Repos.class);
        log.info(repos2.getPath() + " " + repos2.getOwner() + " " + repos2.getName() + " " + repos2.getFork() + " " + repos2.getJsonMemberPrivate() + " " + repos2.getJsonMemberPublic());
      }
    }
  }
}
